package webservice.axis.util;


import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.CalendarDeserializerFactory;
import org.apache.axis.encoding.ser.CalendarSerializerFactory;


public class AxisUtil {

	public static Object callWebService(Object[] paraValueArr,String url,String schemaNamespace,String methodName,String paraNames,String paraTypes,String returnType){
		Object ret = null;
		try {     	
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new java.net.URL(url+"?wsdl"));
			call.setOperationName(methodName);
			String[] paraNameArr = paraNames.split(",");
			String[] paraTypeArr = paraTypes.split(",");
			for(int i=0;i<paraNameArr.length;i++){
				call.addParameter(paraNameArr[i], getType(paraTypeArr[i]), ParameterMode.IN);
			}
			call.setReturnType(getType(returnType));
			
			if(schemaNamespace != null) {
				QName qName = new QName(schemaNamespace,methodName);
				call.setOperationName(qName);
			}
			
			if (call != null){
				ret = call.invoke(paraValueArr);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	private static QName getType(String name){
		if(name==null) name = "";
		name = name.trim().toLowerCase();
		if("string".equals(name)){
			return XMLType.XSD_STRING;
		}else if("int".equals(name)){
			return XMLType.XSD_INT;
		}else{
			return XMLType.XSD_STRING;
		}
	}
	

}
